﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Windows;
using WaterKit.TcpUdp;
using WaterKit.Base;

namespace Auction_Client
{
    /// <summary>
    /// App.xaml 的交互逻辑
    /// </summary>
    public partial class App : Application
    {
        /****************************客户端***************************/
        public static TcpClient client = new TcpClient();
        public static string ip =AppOpr.GetConfigStrings("server_ip");
        public static ushort port = Convert.ToUInt16(AppOpr.GetConfigStrings("server_port"));
        public static ushort timespan = Convert.ToUInt16(AppOpr.GetConfigStrings("timespan"));
        public static bool islogin = false;

        /****************************标的信息***************************/

        /// <summary>
        /// 标的信息字符串
        /// </summary>
        public static string targetStr="";

        /// <summary>
        /// 当前最高价
        /// </summary>
        public static double maxMoney = 0;

        /// <summary>
        /// 单位编号
        /// </summary>
        public static string BidUser = "";

        /// <summary>
        /// 竞拍时限
        /// </summary>
        public static int auctionTime ;
        
        /// <summary>
        /// 递增金额
        /// </summary>
        public static double incrementMoney = 0;

        /// <summary>
        /// 竞拍方式
        /// </summary>
        public static string auctionType = "";

        /// <summary>
        /// 延时拍卖进入下个环节
        /// </summary>
        public static bool canCycle = false;

        /// <summary>
        /// 是否确认标的底价
        /// </summary>
        public static bool isConfirmprice = false;

        /// <summary>
        /// 拍卖会确认标的底价时限,单位:分钟
        /// </summary>
        public static int confirmPriceTime = Convert.ToInt32(new DataTable().Compute(AppOpr.GetConfigStrings("confirmPriceTime"), null));


        /****************************全局事件***************************/
        /// <summary>
        /// 通讯命令类型
        /// </summary>
        public enum cmdType
        {
            /// <summary>
            /// 无效命令
            /// </summary>
            c00,
            /// <summary>
            /// 客户端登录
            /// </summary>
            c01,
            /// <summary>
            /// 登录失败
            /// </summary>
            c02,
            /// <summary>
            /// 登录成功
            /// </summary>
            c03,
            /// <summary>
            /// 竞价
            /// </summary>
            c04,
            /// <summary>
            /// 更新最高价
            /// </summary>
            c05,
            /// <summary>
            /// 竞价失败
            /// </summary>
            c06,
            /// <summary>
            /// 确定标的底价
            /// </summary>
            c07
        }

        /// <summary>
        /// 连接服务器
        /// </summary>
        public static void connectServer()
        {
            try
            {
                App.client.Connect(App.ip, App.port);
            }
            catch (Exception ex)
            {
                Log4C.WriteErrLog(string.Format("连接服务器：{0}:{1} 出错", ip , port), ex);
            }
        }

        /// <summary>
        /// 发送数据至服务端
        /// </summary>
        /// <param name="cmdType"></param>
        /// <param name="Content"></param>
        public static void sendData(App.cmdType cmdType, string Content)
        {
            try
            {
                App.client.Send(cmdType.ToString() + Content);
            }
            catch (Exception ex)
            {
                Log4C.WriteErrLog("客户端发送数据", ex);
            }
        }
    }
}
